/*
 * @lc app=leetcode.cn id=238 lang=golang
 *
 * [238] 除自身以外数组的乘积
 */

// @lc code=start
func productExceptSelf(nums []int) []int {
	n := len(nums)
	left, right := make([]int, n, n), make([]int, n, n)
	left[0], right[n-1] = nums[0], nums[n-1]
	for i := 1; i < n; i++ {
		left[i] = nums[i] * left[i-1]
		right[n-i-1] = nums[n-i-1] * right[n-i]
	}
	ans := make([]int, n, n)
	for i := 0; i < n; i++ {
		if i == 0 {
			ans[i] = right[i+1]
		} else if i == n-1 {
			ans[i] = left[i-1]
		} else {
			ans[i] = left[i-1] * right[i+1]
		}

	}

	return ans
}

// @lc code=end

